/**
 * 文件名称:  DownLoadController.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2017/5/5 01:15
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.base.download;


import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/base/exp")
public class DownLoadController {
    /**
     * 下载文件
     */
    @RequestMapping("/xlsx")
    @ResponseBody
    public void downLoad(HttpServletRequest request, HttpServletResponse response,
                         @RequestParam(name = "fileName", defaultValue = "导出文件") String fileName,
                         @RequestParam(name = "fileType", defaultValue = "xlsx") String fileType,
                         @RequestParam(name = "fileId", defaultValue = "") String fileId
    ) throws Exception {
        this.downXls(fileId,fileType,request,response,fileName,true);
    }


    @RequestMapping("/download/xlsx")
    @ResponseBody
    public void downLoadXlsx(HttpServletRequest request, HttpServletResponse response,
                         @RequestParam(name = "fileName", defaultValue = "导出文件") String fileName,
                         @RequestParam(name = "fileType", defaultValue = "xlsx") String fileType,
                         @RequestParam(name = "fileId", defaultValue = "") String fileId
    ) throws Exception {
        this.downXls(fileId,fileType,request,response,fileName,false);
    }

    private void downXls(String fileId,String fileType,HttpServletRequest request,HttpServletResponse response,
                         String fileName,boolean delFlag) throws Exception {
        if (StringUtils.isEmpty(fileId)) {
            throw new Exception("文件不存在！");
        }
        //获取应用绝对路径
        String path = request.getSession().getServletContext().getRealPath(".");
        String absPath = path + File.separator + "upload" + File.separator + fileId + "." + fileType;
        File file = new File(absPath);
        if (!file.exists()) {
            throw new Exception("文件不存在!");
        }
        // 读到流中
        InputStream inStream = new FileInputStream(file);
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + "." + fileType);
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
            file = new File(absPath);
            if(delFlag) {//true删除文件
                if (file.exists()) {
                    file.delete();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

